﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QuanLyBanHang.DTO;
using System.Data;
using System.Data.SqlClient;
namespace QuanLyBanHang
{
   public class SanPhamDAL
    {
      private static  KetNoiSQL kn = new KetNoiSQL();
     
        public static int ThemSanPham(SanPhamcs sp)
       {
           SqlCommand cmd = new SqlCommand();
           cmd.CommandText = "insert into SanPham values (@MaSp,@TenSP,@DonViTinh,@DonGia,@SoLuongCon)";
           cmd.Parameters.Add("@MaSP", SqlDbType.NVarChar, 50).Value = SanPhamcs.MaSanPham;
           cmd.Parameters.Add("@TenSP", SqlDbType.NVarChar, 50).Value = SanPhamcs.TenSanPham;
           cmd.Parameters.Add("@DonViTinh", SqlDbType.NVarChar, 50).Value = SanPhamcs.DonViTinh;
           cmd.Parameters.Add("@DonGia", SqlDbType.Int).Value = SanPhamcs.DonGia;
           cmd.Parameters.Add("@SoLuongCon", SqlDbType.Int).Value = SanPhamcs.SoLuong;
           return kn.DongThucThi(cmd);

       }
        public static int XoaSanPham(SanPhamcs sp)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Delete SanPham where MaSp = @MaSP";
            cmd.Parameters.Add("@MaSP", SqlDbType.NVarChar, 50).Value = SanPhamcs.MaSanPham;
            return kn.DongThucThi(cmd);

        }
        public static int SuaSanPham(SanPhamcs sp)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "update  SanPham set  TenSP = @TenSP,DonViTinh = @DonViTinh,DonGia = @DonGia,SoLuongCon=@SoLuongCon where MaSp=@MaSp ";
            cmd.Parameters.Add("@MaSP", SqlDbType.NVarChar, 50).Value = SanPhamcs.MaSanPham;
            cmd.Parameters.Add("@TenSP", SqlDbType.NVarChar, 50).Value = SanPhamcs.TenSanPham;
            cmd.Parameters.Add("@DonViTinh", SqlDbType.NVarChar, 50).Value = SanPhamcs.DonViTinh;
            cmd.Parameters.Add("@DonGia", SqlDbType.Int).Value = SanPhamcs.DonGia;
            cmd.Parameters.Add("@SoLuongCon", SqlDbType.Int).Value = SanPhamcs.SoLuong;
            return kn.DongThucThi(cmd);

        }
        public static DataTable LayTatCaThongTinSanPham(SanPhamcs sp)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select * from SanPham";
            return kn.LayDuLieu(cmd);

        }
        public static DataTable LaySanPhamTheoDK(string dk)
        {
            return KetNoiSQL.LayDuLieuSelect("select * from SanPham " + dk);
        }
    
    }
}
